package main

import (
	"fmt"
	"github.com/gogf/gf/net/ghttp"
	"go-learn/go_learn/obs"
	"strings"
)

var ak = "EKYEXDZN81NZKWL5N0VQ"
var sk = "9lvCrqbNxIEfzw7wYNN8mBwofHpaFq0QtDpl3ZIZ"
var endpoint = "http://obs.cn-east-2.myhuaweicloud.com"

var obsClient, _ = obs.New(
	ak,
	sk,
	endpoint,
	obs.WithConnectTimeout(30),
	obs.WithSocketTimeout(60),
	obs.WithMaxConnections(100),
	obs.WithMaxRetryCount(3),
)

func main() {
	if r, err := ghttp.Get("https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/baiduyun@2x-e0be79e69e.png"); err != nil {
		panic(err)
	} else {
		defer r.Close()
		data := r.ReadAllString()

		input := &obs.PutObjectInput{}
		input.Bucket = "oa-test"
		input.Key = "test-foo1.png"
		input.Body = strings.NewReader(data)
		input.ACL = obs.AclPublicReadWrite
		output, err := obsClient.PutObject(input)

		if err == nil {
			fmt.Printf("RequestId:%s\n", output.RequestId)
			fmt.Printf("ETag:%s, StorageClass:%s\n", output.ETag, output.StorageClass)
			fmt.Println(output)
		} else {
			if obsError, ok := err.(obs.ObsError); ok {
				fmt.Println(obsError.Code)
				fmt.Println(obsError.Message)
			} else {
				fmt.Println(err)
			}
		}
	}
}

func main02() {
	input := &obs.GetObjectAclInput{}
	input.Bucket = "oa-test"
	input.Key = "test-foo.png"
	output, err := obsClient.GetObjectAcl(input)
	if err == nil {
		fmt.Printf("RequestId:%s\n", output.RequestId)
		fmt.Printf("Owner.ID:%s\n", output.Owner.ID)
		for index, grant := range output.Grants {
			fmt.Printf("Grant[%d]-Type:%s, ID:%s, URI:%s, Permission:%s\n", index, grant.Grantee.Type, grant.Grantee.ID, grant.Grantee.URI, grant.Permission)
		}
	} else {
		if obsError, ok := err.(obs.ObsError); ok {
			fmt.Println(obsError.Code)
			fmt.Println(obsError.Message)
		} else {
			fmt.Println(err)
		}
	}
}

func main01() {
	input := &obs.SetObjectAclInput{}
	input.Bucket = "oa-test"
	input.Key = "test-foo.png"
	input.Owner.ID = "d113684d89f442fa895034e62bf38bff"
	var grants [3]obs.Grant
	grants[0].Grantee.Type = obs.GranteeGroup
	grants[0].Grantee.URI = obs.GroupAuthenticatedUsers
	grants[0].Permission = obs.PermissionFullControl

	grants[1].Grantee.Type = obs.GranteeUser
	grants[1].Grantee.ID = "拥有者"
	grants[1].Permission = obs.PermissionFullControl

	grants[2].Grantee.Type = obs.GranteeUser
	grants[2].Grantee.ID = "匿名用户"
	grants[2].Permission = obs.PermissionFullControl

	input.Grants = grants[0:3]
	output, err := obsClient.SetObjectAcl(input)
	if err == nil {
		fmt.Printf("RequestId:%s\n", output.RequestId)
	} else {
		if obsError, ok := err.(obs.ObsError); ok {
			fmt.Println(obsError.Code)
			fmt.Println(obsError.Message)
		} else {
			fmt.Println(err)
		}
	}
}
